package com.boot.app.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boot.api.ApiResult;
import com.boot.app.base.domain.BaseMenu;
import com.boot.app.base.service.IBaseMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("base/menu")
public class BaseMenuController {

    private final IBaseMenuService service;

    @Autowired
    public BaseMenuController(IBaseMenuService service){ this.service = service; }

    @GetMapping("{id}")
    public ApiResult<BaseMenu> getMenu(@PathVariable("id") long id){
        BaseMenu menu = service.getById(id);
        return ApiResult.success(menu);
    }

    @GetMapping("list")
    public ApiResult<List<BaseMenu>> getMenuList(){
        List<BaseMenu> menuList = service.list();
        return ApiResult.success(menuList);
    }

    @PostMapping()
    public ApiResult<?> postAndPutMenu(@RequestBody BaseMenu menu){
        service.saveOrUpdate(menu);
        return ApiResult.success();
    }

    @DeleteMapping("{id}")
    public ApiResult<?> deleteMenu(@PathVariable("id") long id){
        service.removeById(id);
        return ApiResult.success();
    }

    @GetMapping("list/{appId}")
    public ApiResult<List<BaseMenu>> getMenuListByAppId(@PathVariable long appId){
        LambdaQueryWrapper<BaseMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseMenu::getAppId, appId);
        List<BaseMenu> menuList = service.list(wrapper);
        return ApiResult.success(menuList);
    }

}
